package com.cb.web.wasf.tag;

import com.cb.web.wasf.DefaultSession;
import com.cb.web.wasf.Request;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.tagext.TryCatchFinally;

/**
 * @author CBO
 */
public class HrefTag extends TagSupport implements TryCatchFinally {

    private String event;
    private boolean async = true;

    public void setEvent(String event) {
        this.event = event;
    }

    @Override
    public int doEndTag() throws JspException {
        StringBuilder sb = new StringBuilder();
        //sb.append(TagHelper.getControllerPath(pageContext));
        sb.append("?" + DefaultSession.SESSION_ID_RPN + "=").append(DefaultSession.getFromPageContext(pageContext).getId());
        sb.append("&" + Request.EVENT_RPN + "=").append(event);
        sb.append("&" + Request.ASYNC_RPN + "=").append(async);
        try {
            pageContext.getOut().print(sb.toString());
        } catch (IOException ioe) {
            throw new JspException(ioe);
        }
        return EVAL_PAGE;
    }

    @Override
    public void doCatch(Throwable throwable) throws Throwable {
        throw throwable;
    }

    @Override
    public void doFinally() {
        event = null;
        async = true;
    }
}
